#include "../include/logindialog.h"
#include "../ui_set/ui_logindialog.h"
#include<QMessageBox>
#include<QSqlQuery>
#include<QRegularExpressionValidator>

LoginDialog::LoginDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LoginDialog)
{
    ui->setupUi(this);

    this->setFixedSize(400,300);
    this->setWindowTitle(tr("登录"));
    ui->pwdLineEdit->setFocus();
    ui->loginBtn->setDefault(true);
    this->constraintInputPassword();

    connect(ui->quitBtn,SIGNAL(clicked(bool)),this,SLOT(reject()));
}

const bool LoginDialog::constraintInputPassword(){
    QRegularExpression numAndLetter("\\w{8,12}");
    QValidator *valdt=new QRegularExpressionValidator(numAndLetter,this);
    ui->pwdLineEdit->setValidator(valdt);
    return true;
}

LoginDialog::~LoginDialog()
{
    delete ui;
}

void LoginDialog::on_loginBtn_clicked()
{
    if(ui->pwdLineEdit->text().isEmpty()){
        QMessageBox::information(this,tr("请输入密码"),tr("请先输入密码在登录！"),QMessageBox::Ok);
        ui->pwdLineEdit->setFocus();
    }
    else{
        QSqlQuery query;
        query.exec("select pwd from password");
        query.next();
        if(query.value(0).toString()==ui->pwdLineEdit->text())
            QDialog::accept();
        else{
            QMessageBox::warning(this,tr("密码错误"),tr("请输入正确密码！"),QMessageBox::Ok);
            ui->pwdLineEdit->clear();
            ui->pwdLineEdit->setFocus();
        }
    }
}

